﻿using System.IO;

namespace RaGE
{
    internal class EListConversation
    {
        public int DialogCount;
        public EDialog[] Dialogs;

        public EListConversation(byte[] bytes)
        {
            var memoryStream = new MemoryStream(bytes);
            var binaryReader = new BinaryReader(memoryStream);

            DialogCount = binaryReader.ReadInt32();
            Dialogs = new EDialog[DialogCount];

            for (var i = 0; i < DialogCount; i++)
            {
                Dialogs[i] = new EDialog();
                Dialogs[i].DialogID = binaryReader.ReadInt32();
                Dialogs[i].DialogName = binaryReader.ReadBytes(128);
                Dialogs[i].QuestionCount = binaryReader.ReadInt32();
                Dialogs[i].Questions = new EQuestion[Dialogs[i].QuestionCount];

                for (var j = 0; j < Dialogs[i].QuestionCount; j++)
                {
                    Dialogs[i].Questions[j] = new EQuestion();
                    Dialogs[i].Questions[j].QuestionID = binaryReader.ReadInt32();
                    Dialogs[i].Questions[j].Control = binaryReader.ReadInt32();
                    Dialogs[i].Questions[j].QuestionTextLength = binaryReader.ReadInt32();
                    Dialogs[i].Questions[j].QuestionText = binaryReader.ReadBytes(Dialogs[i].Questions[j].QuestionTextLength * 2);
                    Dialogs[i].Questions[j].ChoiceQount = binaryReader.ReadInt32();
                    Dialogs[i].Questions[j].Choices = new EChoice[Dialogs[i].Questions[j].ChoiceQount];

                    for (var k = 0; k < Dialogs[i].Questions[j].ChoiceQount; k++)
                    {
                        Dialogs[i].Questions[j].Choices[k] = new EChoice();
                        Dialogs[i].Questions[j].Choices[k].Control = binaryReader.ReadInt32();
                        Dialogs[i].Questions[j].Choices[k].ChoiceText = binaryReader.ReadBytes(132);
                    }
                }
            }
            binaryReader.Close();
            memoryStream.Close();
        }

        public byte[] GetBytes()
        {
            var memoryStream = new MemoryStream(DialogCount);
            var binaryWriter = new BinaryWriter(memoryStream);

            binaryWriter.Write(DialogCount);

            for (var i = 0; i < DialogCount; i++)
            {
                binaryWriter.Write(Dialogs[i].DialogID);
                binaryWriter.Write(Dialogs[i].DialogName);
                binaryWriter.Write(Dialogs[i].QuestionCount);

                for (var j = 0; j < Dialogs[i].QuestionCount; j++)
                {
                    binaryWriter.Write(Dialogs[i].Questions[j].QuestionID);
                    binaryWriter.Write(Dialogs[i].Questions[j].Control);
                    binaryWriter.Write(Dialogs[i].Questions[j].QuestionTextLength);
                    binaryWriter.Write(Dialogs[i].Questions[j].QuestionText);
                    binaryWriter.Write(Dialogs[i].Questions[j].ChoiceQount);

                    for (var k = 0; k < Dialogs[i].Questions[j].ChoiceQount; k++)
                    {
                        binaryWriter.Write(Dialogs[i].Questions[j].Choices[k].Control);
                        binaryWriter.Write(Dialogs[i].Questions[j].Choices[k].ChoiceText);
                    }
                }
            }

            binaryWriter.Close();
            memoryStream.Close();

            return memoryStream.ToArray();
        }


    }
}
